package com.whj.servicemap.remote;

import com.whj.internalcommon.constant.AMapConfigConstant;
import com.whj.internalcommon.response.DirectionResponse;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


/**
 * @Auther: wanghaijun
 * @Date: 2023/1/6 - 01 - 06 - 15:10 
 * @Description: com.whj.servicemap.remote
 */

@Service
@Slf4j
public class MapDirectionClient {

    @Value("${amap.key}")
    private String amapKey;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 根据出发的经纬度获取距离和时间
     * @param depLongitude
     * @param depLatitude
     * @param desLongitude
     * @param desLatitude
     * @return
     */
    public DirectionResponse direction(String depLongitude, String depLatitude, String desLongitude, String desLatitude) {

        //组装url
        StringBuilder urlBuild = new StringBuilder();
        urlBuild.append(AMapConfigConstant.mapUrl);
        urlBuild.append("?");
        urlBuild.append("origin=" + depLongitude + "," + depLatitude);
        urlBuild.append("&");
        urlBuild.append("destination=" + desLongitude + "," + desLatitude);
        urlBuild.append("&");
        urlBuild.append("extensions=all");
        urlBuild.append("&");
        urlBuild.append("output=json");
        urlBuild.append("&");
        urlBuild.append("key=" + amapKey); //key 9edd7a0c343d993fc971ad2afda94dd5
        //urlBuild = https://restapi.amap.com/v3/direction/driving?origin=116.481028,39.989643&destination=116.465302,40.004717&extensions=all&output=json&key=9edd7a0c343d993fc971ad2afda94dd5
        log.info(urlBuild.toString());

        //调用高德的接口
        ResponseEntity<String> directionForEntity = restTemplate.getForEntity(urlBuild.toString(), String.class);
        String directionString = directionForEntity.getBody();

        //解析接口
        DirectionResponse directionResponse = parseDirectionEntity(directionString);
        return directionResponse;
    }

     /**
     * 根据传来的字符串获取指定的参数的数据 解析数据
     * @param directionString
     * @return
     */
    private DirectionResponse parseDirectionEntity(String directionString) {

        DirectionResponse directionResponse = null;

        try {
            //将获取的对象转化为JSON格式
            JSONObject result = JSONObject.fromObject(directionString);
            if(result.has(AMapConfigConstant.STATUS)){
                int status = result.getInt(AMapConfigConstant.STATUS);
                if(status==1){
                    if(result.has(AMapConfigConstant.ROUTE)){
                        JSONObject routeObject = result.getJSONObject(AMapConfigConstant.ROUTE);
                        JSONArray pathsArray = routeObject.getJSONArray(AMapConfigConstant.PATHS);
                        //获取ROUTE 数组中的第一个json
                        JSONObject pathObject = pathsArray.getJSONObject(0);
                        //创建接收对象
                        directionResponse = new DirectionResponse();
                        //设置距离
                        if(pathObject.has(AMapConfigConstant.DISTANCE)){
                            int distance = pathObject.getInt(AMapConfigConstant.DISTANCE);
                            directionResponse.setDistance(distance);
                        }
                        //设置时间
                        if (pathObject.has(AMapConfigConstant.DURATION)){
                            int duration = pathObject.getInt(AMapConfigConstant.DURATION);
                            directionResponse.setDuration(duration);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return directionResponse;
    }
}
